//
//  FriendListVC.m
//  MyDemo
//
//  Created by yuanjiang on 2018/1/29.
//  Copyright © 2018年 yuanjiang. All rights reserved.
//

#import "FriendListVC.h"

@interface FriendListVC ()<UITableViewDelegate,UITableViewDataSource>
/** */
@property(nonatomic,strong) UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *creatDiscussion;

/** */
@property(nonatomic,strong) NSMutableArray *listArray;

@end

@implementation FriendListVC
- (IBAction)creatDiscussionAction:(id)sender {
    [[RCIMClient sharedRCIMClient] createDiscussion:@"讨论组" userIdList:@[@"10000",@"10001",@"10002",@"10003",@"10004"] success:^(RCDiscussion *discussion) {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:@"创建讨论组成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            //获取谈论组列表
            [[RCIMClient sharedRCIMClient] getDiscussion:discussion.discussionId success:^(RCDiscussion *discussion) {
                NSLog(@"%@",discussion);
                [self.listArray addObject:@{@"name":@"胡、申、姚、习、曾",
                                            @"userId":discussion.discussionId,@"token":@""}];
                [self.tableView reloadData];
            } error:^(RCErrorCode status) {
                NSLog(@"%ld",status);
            }];
        }];
        [alertVC addAction:action];
        [self presentViewController:alertVC animated:YES completion:nil];
    } error:^(RCErrorCode status) {
        NSString *str = [NSString stringWithFormat:@"创建讨论组失败--%ld",status];
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertVC addAction:action];
        [self presentViewController:alertVC animated:YES completion:nil];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initData];
    [self.view addSubview:self.tableView];
}


-(void)initData
{
    self.listArray = [NSMutableArray arrayWithObjects:@{@"name":@"胡锦涛",
                                                        @"userId":@"10001",@"token":@"j0FHKYLJUm1glrldWlodHpZlA6KpGYVe4LH/cHsMew2edHXQD1KcksziJ3BG+/5BHqksXgaS5KUOJQrbgWO8mA=="},
                      @{@"name":@"姚明",@"userId":@"10002",@"token":@"KDnvaPFztkANGu2oJR4jqIh2IwcUsRUuyuvPQMjD0ib/LFBPYS+CMYFqSBMANT0/eRxxv2tsf7eU+I0qYwZ/bQ=="},
                      @{@"name":@"习近平",@"userId":@"10003",@"token":@"HUt8v4cg0YIFdPKoa3ZIhpZlA6KpGYVe4LH/cHsMew2edHXQD1KcknI0s7+2uPieKDqEdktBJQLqtpmCk5bmCg=="},
                      @{@"name":@"曾小闲",@"userId":@"10004",@"token":@"GFaL6tvfLUe/G5jUKjfA9Ih2IwcUsRUuyuvPQMjD0ib/LFBPYS+CMa2IXtOIzwfqSqUiriLzLufilav0QU9KPA=="},
                      @{@"name":@"申元江",@"userId":@"10000",@"token":@"IUrYhmZBdlB0LtqeNgxdr5ZlA6KpGYVe4LH/cHsMew2edHXQD1Kckm1sHgwJWmcaDaX7i2TjaJB9VLYIh+CDbg=="}, nil];
}

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellId = @"cellId";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.indentationLevel =  2;
    cell.textLabel.text = self.listArray[indexPath.row][@"name"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.listArray[indexPath.row];
    RCConversationType type = ConversationType_PRIVATE;
    if (indexPath.row>4) {
        type = ConversationType_DISCUSSION;
    }
    
    //新建一个聊天会话View Controller对象,建议这样初始化
    SingleChatVC *chat = [[SingleChatVC alloc] initWithConversationType:type targetId:dic[@"userId"]];
    //设置会话的目标会话ID。（单聊、客服、公众服务会话为对方的ID，讨论组、群聊、聊天室为会话的ID）
    
    //设置聊天会话界面要显示的标题
    chat.title = dic[@"name"];
    //显示聊天会话界面
    [self.navigationController pushViewController:chat animated:YES];
}

-(NSMutableArray *)listArray
{
    if (!_listArray) {
        _listArray = [NSMutableArray array];
    }
    return _listArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
